<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | Controller基础类 v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace app\core\controller;

use app\core\model\EnterpriseModule;
use app\core\model\SiteConfig;
use app\core\model\SiteModule;
use ding\ApiHelper;
use think\App;
use think\Config;
use think\Controller;
use think\exception\HttpResponseException;
use think\Log;

class CoreBaseController extends Controller {
    /**
     * 构造函数
     */
    public function __construct() {
        parent::__construct();
    }

    /**
     * 猜测当前可能的eid
     *
     * @return mixed
     */
    protected function _guessEid() {
        $eid = input("get.eid");
        if (empty($eid)) {
            $eid = cookie(COOKIE_CORE_CURRENT_ENTERPRISE_ID);
        }
        return $eid;
    }

    /**
     * 检查验证码
     *
     * @param string $paramName
     * @param string $id
     */
    protected function _checkCaptcha($paramName = 'code', $id = '') {
        $code = input("param." . $paramName);
        if (empty($code)) {
            $this->error("请输入验证码");
        }
        if (!captcha_check($code, $id, (array)Config::get('captcha'))) {
            //验证失败
            $this->error("图形验证码错误");
        };
    }

    /**
     * 记录调试日志
     * @param $msg
     */
    protected function _d($msg) {
        if (!App::$debug) {
            return;
        }
        $this->_log($msg, 'debug');
    }

    /**
     * 记录日志
     *
     * @param $msg
     * @param string $level
     */
    protected function _log($msg, $level = 'info') {
        if (is_array($msg) || is_object($msg)) {
            $msg = json_encode($msg);
        }
        Log::record('[ ' . $this->request->module() . ' ] ' . $msg, $level);
    }

    /**
     * 错误
     *
     * @param string $msg
     * @param null $url
     * @param string $data
     * @param int $wait
     * @param array $header
     */
    protected function error($msg = '', $url = null, $data = '', $wait = 3, array $header = []) {
        if (App::$debug) {
            $wait = 999999;
        }
        parent::error($msg, $url, $data, $wait, $header);
    }

    /**
     * 获取已经赋值到模板中的变量
     *
     * @param $name
     * @return mixed
     */
    protected function _getAssignedData($name) {
        return $this->view->__get($name);
    }

}